const fs = require('fs');
const path = require('path');
var Config = function () {
    return new Promise(function (rootResolve) {
        new Promise(function (resolve, reject) {
            var oCtx = {
                oConfig: {}
            };
            resolve(oCtx);
        }).then(function (oCtx) {
            return new Promise(function (resolve, reject) {
                // read config.json
                var sConfig = null;
                try {
                    sConfig = fs.readFileSync(path.resolve("config.json"));

                    // parse JSON
                    if (sConfig) {
                        var oConfig = JSON.parse(sConfig, 'utf8');
                        for (const key in oConfig) {
                            if (oConfig.hasOwnProperty.call(oConfig, key)) {
                                oCtx.oConfig[key] = oConfig[key];
                            }
                        }
                        resolve(oCtx);
                    }
                } catch (error) {
                    console.log("read config.json error. " + error.message + error.stack);
                    reject(error);
                }
            });
        }).then(function (oCtx) {
            return new Promise(function (resolve, reject) {
                // Do something
                resolve(oCtx);
            });
        }).then(function (oCtx) {
            return new Promise(function (resolve, reject) {
                // Do something
                rootResolve();
            });
        }).catch(function (error) {
            console.log("config execution error. " + error.message);
        })
    });
}
Config();